﻿using System;
using System.Collections;
using System.Collections.Generic;
//using IBatisNet.DataAccess;
using Wsitech.Model;
using Wsitech.Utility;
using System.IO;
using System.Web;

namespace Wsitech.BLL
{
    /// <summary>
    /// 功能：     AttachInfo业务逻辑实现
    /// 作者：     LI
    /// 日期：     5/16/2012
    /// </summary>
    public class AttachInfoBLL
    {
        private Wsitech.Persistence.Interface.IAttachInfoDao dao = null;

        private AttachInfoBLL()
        {
            dao = Wsitech.Persistence.Dao.AttachInfoSqlMapDao.Instance;
        }

        #region （懒汉）单例模式
        private static AttachInfoBLL instance = null;
        private static object syncRoot = new object();
        public static AttachInfoBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new AttachInfoBLL();
                        }
                    }
                }
                return instance;
            }
        }
        #endregion

        public static readonly string DEFAULT_VIDEO_COVER = "/!images/videopic.jpg";

        #region	基本功能 不能修改


        /// <summary>
        /// 根据主键得到明细
        /// </summary>
        /// <returns></returns>
        public AttachInfo GetDetail(System.String attachid)
        {
            try
            {
                return dao.GetDetail(attachid);
            }
            catch
            {
                throw;
            }
        }


        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public IList<AttachInfo> GetList()
        {
            try
            {
                IList<AttachInfo> list = dao.GetList();
                return list;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private IList<AttachInfo> GetListByParams(object TopCounts, AttachInfo attachinfo)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                IList<AttachInfo> list = dao.GetListByParams(ht);
                return list;
            }
            catch
            {
                throw;
            }
        }



        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="attachinfo"></param>
        /// <returns></returns>
        public object Add(AttachInfo attachinfo)
        {
            try
            {
                LogInfoBLL.Instance.AddLog(UtilityEnum.Operation.Add, GetDetail(attachinfo.AttachId));
                return dao.Add(attachinfo);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="attachinfo"></param>
        /// <returns></returns>
        public int Update(AttachInfo attachinfo)
        {
            try
            {
                LogInfoBLL.Instance.AddLog(UtilityEnum.Operation.Update, GetDetail(attachinfo.AttachId));
                return dao.Update(attachinfo);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="AttachId"></param>
        /// <returns></returns>
        public int Delete(System.String attachid)
        {
            try
            {
                return dao.Delete(attachid);
            }
            catch
            {
                throw;
            }
        }

        #endregion


        #region 扩展功能

        #endregion

        #region 根据信息编号取所有附件信息
        /// <summary>
        /// 根据信息编号取所有附件信息
        /// </summary>
        /// <param name="textinfoid"></param>
        /// <returns></returns>
        public IList<AttachInfo> GetListByInfoId(string textinfoid)
        {
            Hashtable ht = new Hashtable();
            ht.Add("infoid", textinfoid);
            return dao.GetListByParams(ht);
        }
        #endregion

        #region 根据编号信息获取附件信息
        /// <summary>
        /// 根据编号信息获取所有可用附件信息
        /// </summary>
        /// <param name="textinfoid"></param>
        /// <returns></returns>
        public IList<AttachInfo> GetActiveListByInfoId(string textinfoid)
        {
            Hashtable ht = new Hashtable();
            ht.Add("infoid", textinfoid);
            ht.Add("state", (int)UtilityEnum.State.Enable);
            return dao.GetListByParams(ht);
        }




        #endregion

        #region 添加附件
        /// <summary>
        /// 添加附件
        /// </summary>
        /// <param name="textinfoid"></param>
        /// <param name="description"></param>
        /// <param name="type"></param>
        /// <param name="sourcePath"></param>
        public void AddAttach(string textinfoid, string description, UtilityEnum.AttachType type, string sourcePath)
        {
            AttachInfo attach = new AttachInfo();

            attach.AttachId = Guid.NewGuid().ToString();
            attach.Description = description;
            attach.TextInfoId = textinfoid;
            attach.SourcePath = sourcePath;
            attach.SortIndex = dao.GetMaxIndexByInfoId(textinfoid) + 1;
            attach.State = (int)UtilityEnum.State.Enable;
            attach.AttachType = (int)type;
            attach.ContentLength = "";

            attach.CreateTime = DateTime.Now;
            attach.UpdateTime = DateTime.Now;

            Add(attach);
        }
        public void AddVideoAttach(string textinfoid, string description, UtilityEnum.AttachType type, string videoPath, string videoLength)
        {
            AttachInfo attach = new AttachInfo();

            attach.AttachId = Guid.NewGuid().ToString();
            attach.Description = description;
            attach.TextInfoId = textinfoid;
            attach.SourcePath = videoPath;
            attach.SortIndex = dao.GetMaxIndexByInfoId(textinfoid) + 1;
            attach.State = (int)UtilityEnum.State.Enable;
            attach.AttachType = (int)type;
            attach.ContentLength = videoLength;

            attach.CreateTime = DateTime.Now;
            attach.UpdateTime = DateTime.Now;

            Add(attach);
        }


        #endregion

        #region 修改状态
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="attachid"></param>
        /// <param name="state"></param>
        public void ChangeState(string attachid, UtilityEnum.State state)
        {
            AttachInfo attach = dao.GetDetail(attachid);
            if (attach != null)
            {
                attach.State = (int)state;

                attach.UpdateTime = DateTime.Now;

                Update(attach);
            }
        }

        #endregion




        #region 返回图片缩略图的路径
        /// <summary>
        /// 返回图片缩略图的路径
        /// </summary>
        /// <param name="imgPath"></param>
        /// <returns></returns>
        public string GetThumbPath(string imgPath)
        {
            string ext = Path.GetExtension(imgPath);
            return imgPath.Replace(ext, "_thumb" + ext);
        }
        #endregion








        /// <summary>
        /// 返回视频的封面截图路径
        /// </summary>
        /// <param name="videoPath"></param>
        /// <returns></returns>
        public string GetVideoCoverPath(string videoPath)
        {
            string ext = Path.GetExtension(videoPath);
            return videoPath.Replace(ext, ".jpg");
        }

        public String GetFirstVideoCover(string textinfoid)
        {
            IList<AttachInfo> list = GetActiveListByInfoId(textinfoid);
            foreach (AttachInfo attach in list)
            {
                if (attach.AttachType == (int)UtilityEnum.AttachType.Video)
                {
                    string file = GetVideoCoverPath(attach.SourcePath);
                    string thumbFile = GetThumbPath(file);
                    //优先返回缩略图
                    if (File.Exists(HttpContext.Current.Server.MapPath(thumbFile)))
                    {
                        return thumbFile;
                    }
                    //没有缩略图则返回大图
                    else if (File.Exists(HttpContext.Current.Server.MapPath(file)))
                    {
                        return file;
                    }
                }
            }
            //都没有则返回默认图片
            return DEFAULT_VIDEO_COVER;
        }


        /// <summary>
        ///删除附件
        /// </summary>
        /// <param name="attachid"></param>
        public void DeleteAttach(string attachid)
        {

            AttachInfo attach = GetDetail(attachid);
            if (attach != null)
            {
                //删除记录
                dao.Delete(attachid);

                string file = HttpContext.Current.Server.MapPath(attach.SourcePath);
                if (attach.AttachType == (int)UtilityEnum.AttachType.Video)
                {
                    //删除视频封面
                    string cover = GetVideoCoverPath(file);
                    if (File.Exists(cover))
                    {
                        File.Delete(cover);
                    }
                }
                //删除主要文件
                if (File.Exists(file))
                {
                    File.Delete(file);
                }

            }
        }
    }
}
